/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Business;

import DataAccess.RegisterDataAccess;
import Entities.EnrollEventEntities;
import Entities.EventEntities;
import Entities.RegisterEntities;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author vjphl
 */
@ManagedBean
@RequestScoped
public class RegisterBean {

    List<RegisterEntities> registerList;
    List<EventEntities> eventList;

    RegisterEntities registerEntities;
    EnrollEventEntities enrollEventEntities;
    private String registerLogin;

    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);


    
    /**
     * Creates a new instance of RegisterBean
     */
    public RegisterBean() {
        
        registerList = new ArrayList<RegisterEntities>();
        registerEntities = new RegisterEntities();
        enrollEventEntities = new EnrollEventEntities();
        registerLogin = (String) session.getAttribute("registerID");
        getUserbyId();
        getEventbyUser();
        //registerLogin = "1";
    }
    
    public String insertRegister(){
        try {
            RegisterDataAccess registerDataAccess = new RegisterDataAccess();
            if (registerDataAccess.insertRegister(registerEntities)) {
                FacesMessage error = new FacesMessage("Register Successfull");
                FacesContext.getCurrentInstance().addMessage(null, error);
                return "InsertSuccess";
            }

        } catch (Exception ex) {
            Logger.getLogger(RegisterBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "InsertFalse";
    }
    
    public void checkLogin(){
         try {
            RegisterDataAccess registerDataAccess = new RegisterDataAccess();
            
            if (registerDataAccess.checkLogin(registerEntities)) {
                registerLogin = registerEntities.getUserName();
                session.setAttribute("registerID", registerEntities.getUserName());
                registerLogin = (String) session.getAttribute("registerID");
                //registerEvent();
                //FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/ViewEvent.xhtml");
            }
            else{
                //registerEvent();
                FacesMessage error = new FacesMessage("Login False");
                FacesContext.getCurrentInstance().addMessage(null, error);
                if (FacesContext.getCurrentInstance().isPostback()) {
                    FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/Register.xhtml");
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void checkSession(){
        try {
            if(session == null){
                FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/Register.xhtml");
                session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
                session.setAttribute("registerID", "");
            }
        } catch (IOException ex) {
                Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void getUserbyId() {
        try {
            if (FacesContext.getCurrentInstance().isPostback()) {
                if (registerLogin!=null) {
                    //FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/ViewEvent.xhtml?CatId="+CatId);
                }
            }
            RegisterDataAccess registerDataAccess = new RegisterDataAccess();
            registerList = registerDataAccess.getRegisterById(registerLogin);
            registerEntities = registerList.get(0);

        } catch (Exception ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void getEventbyUser() {
        try {
            if (FacesContext.getCurrentInstance().isPostback()) {
                if (registerLogin!=null) {
                    //FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/ViewEvent.xhtml?CatId="+CatId);
                }
            }
            String EventId =FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("eventId");
            RegisterDataAccess registerDataAccess = new RegisterDataAccess();
            eventList = registerDataAccess.getEventByUser(registerLogin);

        } catch (Exception ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void registerEvent(){
        try {
            if (session.getAttribute("registerID")!= null) {
                String eventid = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("regisEvent");
                String adherence = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("readherence");
                RegisterDataAccess registerDataAccess = new RegisterDataAccess();
                if (eventid!=null) {
                    if (adherence== null) {
                    adherence = "Presentation";
                    FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/UploadSlide.xhtml");
                    }
                }
                if (eventid != null && adherence != null) {
                    if (registerDataAccess.registerEvent(eventid,"1",adherence)) {
                    FacesMessage error = new FacesMessage("Register Event Success");
                    FacesContext.getCurrentInstance().addMessage(null, error);
                    } 
                }
            }
            else{
                FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/Register.xhtml");
            }
            
        } catch (Exception ex) {
            Logger.getLogger(RegisterBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void updateEvent(){
        try {
            RegisterDataAccess registerDataAccess = new RegisterDataAccess();
            if (registerDataAccess.updateRegister(registerEntities)) {
                FacesMessage error = new FacesMessage("Update Successfull");
                FacesContext.getCurrentInstance().addMessage(null, error);
            }

        } catch (Exception ex) {
            Logger.getLogger(RegisterBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void urlRegister(){
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/Register.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void urlViewProfile(){
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/UserProfile.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void urlEditRegister(){
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("../faces/EditProfile.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(EventBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public List<RegisterEntities> getRegisterList() {
        return registerList;
    }

    public void setRegisterList(List<RegisterEntities> registerList) {
        this.registerList = registerList;
    }
    
    public RegisterEntities getRegisterEntities() {
        return registerEntities;
    }

    public void setRegisterEntities(RegisterEntities registerEntities) {
        this.registerEntities = registerEntities;
    }
    
    public String getRegisterLogin() {
        return registerLogin;
    }

    public void setRegisterLogin(String registerLogin) {
        this.registerLogin = registerLogin;
    }
    
    
    public HttpSession getSession() {
        return session;
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }
    
    public List<EventEntities> getEventList() {
        return eventList;
    }

    public void setEventList(List<EventEntities> eventList) {
        this.eventList = eventList;
    }
}
